package com.lu.manage.modular.system.controller;

import com.lu.manage.core.base.controller.BaseController;
import com.lu.manage.core.common.constant.SystemConstant;
import com.lu.manage.core.common.exception.SysLogExcepetion;
import com.lu.manage.core.common.node.ZTreeNode;
import com.lu.manage.core.tips.SuccessTip;
import com.lu.manage.modular.system.model.Dict;
import com.lu.manage.modular.system.service.DictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Project LuBoot
 * @Author: zhanglu
 * @Date: 2019-11-17 00:46:19
 * @Description: 基础字典 控制器
 */
@Controller
@RequestMapping("/system/dict")
public class DictController extends BaseController {

    private static final String PREFIX = "/modular/system/dict";

    @Autowired
    private DictService dictService;


/*******************************跳转页面相关*******************************/

    /**
    * 首页
    * @return
    */
    @GetMapping("")
    public String index(String dictTypeId, String dictTypeName, Model model){
        model.addAttribute("dictTypeId", dictTypeId);
        model.addAttribute("dictTypeName", dictTypeName);
        return PREFIX + "/index.html";
    }

    /**
    * 新增
    * @return
    */
    @GetMapping("/toAdd")
    public String toAdd() {
        return PREFIX + "/add.html";
    }

    /**
    * 修改
    * @return
    */
    @GetMapping("/toUpdate")
    public String toUpdate(String id, Model model) throws SysLogExcepetion {
        Dict obj = dictService.getById(id);
        try {
            if(obj.getPid().equals(SystemConstant.pid)){
                obj.setPname(SystemConstant.P_NAME);
            }else {
                Dict pObj = dictService.getById(obj.getPid());
                obj.setPname(pObj.getName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("obj", obj);
        return PREFIX + "/update.html";
    }

/*******************************操作按钮相关*******************************/

    /**
     * 添加
     */
    @RequestMapping("/add")
    @ResponseBody
    @RequiresPermissions(value = "/system/dict/add")
    public Object add(Dict dict) throws SysLogExcepetion {
        dictService.addObj(dict);
        return new SuccessTip();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ResponseBody
    @RequiresPermissions(value = "/system/dict/update")
    public Object update(Dict dict) throws SysLogExcepetion {
        dictService.updateObj(dict);
        return new SuccessTip();
    }

    /**
     * 删除&批量
     */
    @PostMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = "/system/dict/delete")
    public Object delete(String id) throws SysLogExcepetion {
        dictService.deleteObj(id);
        return new SuccessTip();
    }

    /**
     * 修改状态
     * @param id
     * @param state
     * @return
     * @throws SysLogExcepetion
     */
    @PostMapping("/updateState")
    @ResponseBody
    @RequiresPermissions(value = "/system/dict/updateState")
    public Object updateState(String id, Integer state) throws SysLogExcepetion {
        dictService.updateState(id, state);
        return new SuccessTip();
    }

    /**
     * 排序
     * @return
     * @throws SysLogExcepetion
     */
    @PostMapping("/sort")
    @ResponseBody
    @RequiresPermissions(value = "/system/dict/sort")
    public Object sort(String nodes) throws SysLogExcepetion {
        dictService.sort(nodes);
        return new SuccessTip();
    }

/*******************************查询数据相关*******************************/

   /**
    * 分页查询
    */
    @GetMapping("/listPage")
    @ResponseBody
    public Object listPage(@RequestParam Map<String, Object> map) throws SysLogExcepetion {
        return dictService.listPage(map);
    }

    /**
     * 字典列表 treeTable
     */
    @GetMapping("/treeTable")
    @ResponseBody
    public Object treeTable(String dictTypeId, String searchWords) throws SysLogExcepetion {
        List<Dict> dicts = dictService.treeTable(dictTypeId, searchWords);
        return new SuccessTip(dicts);
    }

    /**
     * 字典选择&排序树
     * @return
     */
    @GetMapping("/dictTree")
    @ResponseBody
    public Object dictTree(){
        List<ZTreeNode> nodes = this.dictService.dictTree();
        nodes.add(ZTreeNode.createParent());
        return nodes;
    }

}
